package com.moji.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.moji.common.base.ApiResult;
import com.moji.entity.MjAboutUs;
import com.moji.entity.MjHomepage;
import com.moji.entity.vo.CommonHomePageDTO;
import com.moji.entity.vo.CommonHomePageSubTitleDTO;
import com.moji.service.MjAboutUsService;
import com.moji.service.MjHomePageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.util.TextUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 首页相关 前端控制器
 * </p>
 *
 * @author myg
 * @since 2024-07-23
 */
@RestController
@RequestMapping("api/homepage")
@Api(tags = "首页相关api接口", value = "首页相关api接口")
public class MjHomepageController {
    /**
     * 服务对象
     */
    @Resource
    private MjHomePageService mjHomePageService;
    @Resource
    private MjAboutUsService mjAboutUsService;

    /**
     * 查询首页数据数据
     *
     * @param
     * @return
     */
    @PostMapping("/queryByPage")
    @ApiOperation(value = "分页查询首页相关数据，type非必传")
    public ApiResult<List<MjHomepage>> queryByPage(@RequestBody CommonHomePageDTO commonHomePageDTO) {
        List<MjHomepage> mjBannerMyPage = mjHomePageService.queryByPage(commonHomePageDTO);
        return ApiResult.ok(mjBannerMyPage);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("/detail")
    @ApiOperation(value = "根据id查看基础信息详情",response = MjHomepage.class)
    public ApiResult<MjHomepage> queryById(@RequestParam("id") Long id) {
        return ApiResult.ok(this.mjHomePageService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param mjBasicInfo 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    @ApiOperation(value = "增加网站基本信息",response = MjHomepage.class)
    public ApiResult<MjHomepage> add(@Validated @RequestBody MjHomepage mjBasicInfo) {
        return ApiResult.ok(this.mjHomePageService.insert(mjBasicInfo));
    }

    /**
     * 编辑数据
     *
     * @param mjBasicInfo 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    @ApiOperation(value = "修改网站基本信息",response = MjHomepage.class)
    public ApiResult<MjHomepage> edit(@Validated @RequestBody MjHomepage mjBasicInfo) {
        return ApiResult.ok(this.mjHomePageService.update(mjBasicInfo));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除网站基本信息",response = Boolean.class)
    public ApiResult<Boolean> deleteById(@RequestParam("id")Long id) {
        return ApiResult.ok(this.mjHomePageService.deleteById(id));
    }

    /**
     * 编辑数据
     *
     * @param
     * @return 编辑结果
     */
    @PostMapping("/editSubtitle")
    @ApiOperation(value = "修改网站subtitle",response = Boolean.class)
    public ApiResult<Boolean> edit(@Validated @RequestBody CommonHomePageSubTitleDTO commonHomePageSubTitleDTO) {
        if(commonHomePageSubTitleDTO.getType()!=null)
        {
            if(commonHomePageSubTitleDTO.getType()==2)
            {
                if(!TextUtils.isEmpty(commonHomePageSubTitleDTO.getAboutUsType()))
                {
                    LambdaQueryWrapper<MjAboutUs> eq = new LambdaQueryWrapper<MjAboutUs>().eq(MjAboutUs::getType, commonHomePageSubTitleDTO.getAboutUsType());
                    List<MjAboutUs> mjAboutUsList = mjAboutUsService.list(eq);
                    if (mjAboutUsList != null) {
                        for (int i = 0; i < mjAboutUsList.size(); i++) {
                            MjAboutUs mjAboutUs = mjAboutUsList.get(i);
                            mjAboutUs.setSubTitle(commonHomePageSubTitleDTO.getTypeSubtitle());
                            mjAboutUsService.updateById(mjAboutUs);
                        }

                    }
                    return ApiResult.ok(Boolean.TRUE);
                }

            }
            LambdaQueryWrapper<MjHomepage> eq = new LambdaQueryWrapper<MjHomepage>().eq(MjHomepage::getType, commonHomePageSubTitleDTO.getType());
            List<MjHomepage> list = mjHomePageService.list(eq);
            if (list != null) {
                for (int i = 0; i < list.size(); i++) {
                    MjHomepage mjHomepage = list.get(i);
                    mjHomepage.setTypeSubtitle(commonHomePageSubTitleDTO.getTypeSubtitle());
                    mjHomePageService.updateById(mjHomepage);
                }

            }
            return ApiResult.ok(Boolean.TRUE);

        }

        return ApiResult.ok(Boolean.FALSE);
    }

}

